var log = false;

function addQuery() {
    
    var q = $("#query").val();
    var tid = $("#ntype").val();
    var nto = $("#nto").val();
    var user = $("#login").val();
    var pass = $("#password").val();
    $("#query").attr("disabled", "disabled");
    $("#ntype").attr("disabled", "disabled");
    $("#nto").attr("disabled", "disabled");
    $("#try").attr("disabled", "disabled");
    $("#add").attr("disabled", "disabled");
    $("#login").attr("disabled", "disabled");
    $("#password").attr("disabled", "disabled");
    $("#load").fadeTo(600, 1);
            
    $.post("add.php",
            {
                query : q,
                ntype : tid,
                nto : nto,
                user : user,
                pass : pass
            },
            function(data) {
    
                status = $(data).find("Response").attr("status");
                msg = $(data).find("Response").attr("msg");
                if (status == 0) {
                    
                    alert(msg);
                    $("#query").val("");
                    $("#nto").val("");
                    if (log) {
                        
                        updateQueries();
                    }
                } else {
                    
                    if (msg == undefined) {
                        
                        msg = "";
                    }
                    alert("Não foi possível fazer a inclusão. Tente novamente.\n" + msg);
                }
                
                $("#query").removeAttr("disabled");
                $("#ntype").removeAttr("disabled");
                $("#nto").removeAttr("disabled");
                $("#try").removeAttr("disabled");
                $("#add").removeAttr("disabled");
                $("#login").removeAttr("disabled");
                $("#password").removeAttr("disabled");
                $("#load").fadeTo(600, 0);
            });
}

function doLogIn() {
    
    var user = $("#user").val();
    var pass = $("#pass").val();
    $.post("log.php",
            {
                user : user,
                pass : pass            
            },
            function(data) {

                if (data == "ok") {
                    
                    $("#log_div").html("<span style='font-face: Arial; color: #280099; font-size: 18px'> Usu&aacute;rio: " + user +" <a href='doLogOut.php'>Logout</a><br >Strings de busca<br ><div id='div_queries'></div>");
                    updateQueries()
                    eval("log = true");
                } else {
                    
                    alert("Usuário e/ou senha inválidos");
                }
            });
}

function delQuery(qid) {
    
    $.get("delQuerie.php",
            {
                qid : qid
            },
            function(data) {
                
                var r = $(data).find("Response").attr("status");
                if (r == "0") {
                    
                    updateQueries();
                }
            });
}

function doQuery() {
    
    $("#buscar").attr("disabled", "disabled");
    $("#doquery").attr("disabled", "disabled");
    $("#clean").attr("disabled", "disabled");
    $("#res_query").html("").append("<img id='load_query' src='loading.gif' style='opacity;0%' />");
    $("#load_query").fadeTo(600, 1);
    $.get("doQuery.php",
            {
                query : $("#buscar").val()
            },
            function (data, resp){

                $("#load_query").fadeTo(600, 0);
                $("#res_query").html("");
                $("#res_query").append("<span style='font-face: Arial; color: #280099; font-size: 18px'>Resultados:<br /></span>");
                $(data).find("Document").each(function(){
                    
                    $("#res_query").append()
                });
                $("#buscar").removeAttr("disabled");
                $("#doquery").removeAttr("disabled");
                $("#clean").removeAttr("disabled");
            });
}

function updateQueries() {
    
    $.get("getQueries.php",
            function(data) {

                $("#conta").remove();
                $("#conta_msg").remove();
                $("#div_queries").html("");
                $(data).find('Querie').each(function() {

                    qid = $(this).attr('qid');
                    $("#div_queries").append("<span style='font-face: Arial; color: #280099; font-size: 18px' id='qid" + qid + "'>"
                        + $(this).text() +" <a href=\"javascript:delQuery('"+ qid + "');\">Excluir</a></span><br />")
                });
            });
}
